package com.ruoyi.web.controller.pack;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pack.domain.PackWarehouseFence;
import com.ruoyi.pack.service.IPackWarehouseFenceService;

/**
 * 仓库电子围栏Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/pack/warehouseFence")
public class PackWarehouseFenceController extends BaseController
{
    @Autowired
    private IPackWarehouseFenceService packWarehouseFenceService;

    /**
     * 查询仓库电子围栏列表
     */
    @GetMapping("/fence/{warehouseId}")
    public AjaxResult getWareHouseFence(@PathVariable("warehouseId") Long warehouseId)
    {
        return success(packWarehouseFenceService.selectPackWarehouseFenceByWarehouseId(warehouseId));
    }

    /**
     * 获取仓库电子围栏详细信息
     */
//    @PreAuthorize("@ss.hasPermi('pack:warehouse:fence:query')")
//    @GetMapping("/fence/{fenceId}")
//    public AjaxResult getInfo(@PathVariable("fenceId") Long fenceId)
//    {
//        return success(packWarehouseFenceService.selectPackWarehouseFenceByFenceId(fenceId));
//    }

    /**
     * 新增仓库电子围栏
     */
    @PreAuthorize("@ss.hasPermi('pack:warehouse:fence:add')")
    @Log(title = "仓库电子围栏", businessType = BusinessType.INSERT)
    @PostMapping("/fence")
    public AjaxResult add(@Validated @RequestBody PackWarehouseFence packWarehouseFence)
    {
        packWarehouseFence.setCreateBy(getUsername());
        return toAjax(packWarehouseFenceService.insertPackWarehouseFence(packWarehouseFence));
    }

    /**
     * 修改仓库电子围栏
     */
    @PreAuthorize("@ss.hasPermi('pack:warehouse:fence:edit')")
    @Log(title = "仓库电子围栏", businessType = BusinessType.UPDATE)
    @PutMapping("/fence")
    public AjaxResult edit(@Validated @RequestBody PackWarehouseFence packWarehouseFence)
    {
        packWarehouseFence.setUpdateBy(getUsername());
        return toAjax(packWarehouseFenceService.updatePackWarehouseFence(packWarehouseFence));
    }

    /**
     * 删除仓库电子围栏
     */
    @PreAuthorize("@ss.hasPermi('pack:warehouse:fence:remove')")
    @Log(title = "仓库电子围栏", businessType = BusinessType.DELETE)
    @DeleteMapping("/fence/{fenceIds}")
    public AjaxResult remove(@PathVariable Long[] fenceIds)
    {
        return toAjax(packWarehouseFenceService.deletePackWarehouseFenceByFenceIds(fenceIds));
    }

    /**
     * 设置仓库围栏
     */
    @PreAuthorize("@ss.hasPermi('pack:warehouse:fence:set')")
    @Log(title = "设置仓库围栏", businessType = BusinessType.UPDATE)
    @PostMapping("/{warehouseId}/fence")
    public AjaxResult setWarehouseFence(@PathVariable("warehouseId") Long warehouseId, @RequestBody List<PackWarehouseFence> fenceList)
    {
        // 设置创建者信息
        if (fenceList != null && !fenceList.isEmpty()) {
            for (PackWarehouseFence fence : fenceList) {
                fence.setCreateBy(getUsername());
                fence.setWarehouseId(warehouseId);
            }
        }
        return toAjax(packWarehouseFenceService.setWarehouseFence(warehouseId, fenceList));
    }
}
